package DataSetPackage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;


public class DataSet {

	ArrayList<Attribute> attributes;
	
	public ArrayList<Attribute> getAttributes() {
		return attributes;
	}
	
	public void setAttributes(ArrayList<Attribute> attrs){
		attributes = attrs;
	}

	ArrayList<int[]> data;

	public DataSet() {
		attributes = new ArrayList<Attribute>();
		data = new ArrayList<int[]>();
	}
	public int getNumberOfIntances()
	{
		return data.size();
	}
	public int getNumberOfAttributes()
	{
		return attributes.size();
	}
	
	
	public Attribute getAttribute(int i)
	{
		return attributes.get(i);
	}
	
	public Iterator<int[]> getDataIterator()
	{
		return data.iterator();
	}
	
	public String[] getColumnName() {
		String[] cols = new String[attributes.size()];
		int i = 0;
		for (Iterator<Attribute> iterator = attributes.iterator(); iterator.hasNext(); i++) {
			Attribute attribute = (Attribute) iterator.next();
			cols[i] = attribute.name;

		}
		
		return cols;
	}

	public String[][] getRawData() {
		String[][] values = new String[data.size()][];
		int i = 0;
		for (Iterator<int[]> iterator = data.iterator(); iterator.hasNext(); i++) {
			int[] row = (int[]) iterator.next();
			values[i] = new String[row.length];
			for (int j = 0; j < row.length; j++) {
				if (row[j] != -1)
					values[i][j] = attributes.get(j).nominals.get(row[j]);
			}
		}
		return values;
	}

	public int[] getRow(int i)
	{
		return data.get(i);
	}
	public DataSet subData(int start, int n)
	{
		DataSet dataset = new DataSet();
		dataset.attributes = attributes;
		int end = start+n;
		for(int i=start;i<end;i++)
		{
			dataset.data.add(data.remove(start));
		}
		return dataset;
	}
	public DataSet randomSubData(int n, long seed)
	{
		Random random = new Random(seed);
		DataSet dataset = new DataSet();
		dataset.attributes = attributes;
		for(int i=0;i<n;i++)
		{
			int index = random.nextInt(getNumberOfIntances());
			dataset.data.add(data.remove(index));
		}
		return dataset;
	}
	public void randomize(long seed)
	{
		Random random = new Random(seed);
		for(int i=getNumberOfIntances();i>=1;i--)
		{
			int index = random.nextInt(i);
			data.add(data.remove(index));
		}
		data.add(data.remove(0));
	}
	public DataSet duplicateData()
	{
		DataSet dataset = new DataSet();
		dataset.attributes = attributes;
		dataset.data.addAll(data);
		return dataset;
	}

	public void addAttribute(Attribute attr) {
		// TODO Auto-generated method stub
		attributes.add(attr);
	}

	public void setData(ArrayList<int[]> dat) {
		// TODO Auto-generated method stub
		data = dat;
	}
}
